for idx,val in ipairs(arg) do
    print(idx,val)
end

function foo(a)
	print("foo", a)
	return coroutine.yield(2 * a)
end

co = coroutine.create(function ( a, b )
	print("co-body", a, b)
	local r = foo(a + 1)
	print("co-body", r)
	local r, s = coroutine.yield(a + b, a - b)
	print("co-body", r, s)
	return b, "end"
end)

print("main", coroutine.resume(co, 1, 10))
print("main", coroutine.resume(co, "r"))
print("main", coroutine.resume(co, "x", "y"))
print("main", coroutine.resume(co, "x", "y"))



function runAsyncFunc( func, ... )
	local current = coroutine.running
	func(function (  )
		coroutine.resume(current)
	end, ...)
	coroutine.yield()
end


bob = {}
bob.walkto = function(self, a, callBack)
    print("self", self)
    print("bob walk to")
    a:say("welcome")
    if callBack then
        callBack()
    end
end

bob.say = function(self, words)
    print("bob say ",words)
end
jane = {}
jane.say = function(self, words)
    print("jane say ",words)
end



local co = coroutine.create(function (  )
	runAsyncFunc(bob.walkto, jane)
	runAsyncFunc(bob.say, "hello")
	jane:say("hello")
end)


print("co",coroutine.resume(co))
print("co",coroutine.resume(co))
print("co",coroutine.resume(co))

produceFunc = function()
    for i=1,100,3 do
        local value = io.read()
        print("produce: ", value)
        print("i", i)
        coroutine.yield(value)        --返回生产的值
    end
end

consumer = function(p)
    while true do
        local status, value = coroutine.resume(p);        --唤醒生产者进行生产
        print("consume: ", value)
    end
end

producer = coroutine.create(produceFunc)
consumer(producer)
